package com.cloud.usercenter.controller;

import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.User_account;
import com.cloud.commonapi.model.User_info;
import com.cloud.usercenter.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 陈楚城
 * @date 2019/9/27 8:30
 * 账户信息
 */
@Api(description = "系统账户接口")
@RestController
@RequestMapping("usercontroller/account")
public class AccountController {

    @Autowired
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @PostMapping("/users")
    @ApiOperation(value = "账户注册" ,notes = "")

    public Result register( User_account user_account,  User_info user_info, HttpSession session){
       if(userService.getUser_accountbyAccount(user_account.getAccount())!=null){
          return Result.build(400,"此账户已经存在");
       }
        //加密密码
       user_account.setPassword(bCryptPasswordEncoder.encode(user_account.getPassword()));
        if(userService.add(user_account) > 0) {
            System.out.println(user_account.getId());
            //增加用户信息记录
            //User_info user_info=new User_info();
            user_info.setAccountId(user_account.getId());
            //user_info.setUsername("chen");//不为空
            user_info.setCredit(59);
            user_info.setLevel("不合格");
            userService.addUser_info(user_info);
            return Result.build(200, "注册成功");
        }
        else
            return Result.build(400,"注册失败");
    }
    @PostMapping("/users/login")
    @ApiOperation(value = "账户登录",notes = "")

    public Result login(@RequestParam String account, @RequestParam String password, HttpSession session,HttpServletResponse response){
       User_account user_account=userService.getUser_accountbyAccount(account);
        if(user_account!=null) {
            if(!bCryptPasswordEncoder.matches(password,user_account.getPassword())){
                return Result.build(400, "密码校验失败");
            }else {
                session.setAttribute("loginId", user_account.getId());
                session.setAttribute("loginAccount", user_account.getAccount());
                System.out.println(session.getId());
                //存入cookie
//                Cookie cookie=new Cookie("account",user_account.getAccount());
//                cookie.setMaxAge(60 * 60 * 24);
//                response.addCookie(cookie);
//                response.setHeader("Access-Control-Allow-Origin", "null");
                return Result.build(200, "登录成功");
            }
        }else
            return Result.build(400,"账户不存在");
    }

    @PutMapping("/users")
    @ApiOperation(value = "修改密码" ,notes = "")

    public Result Update(@PathVariable String password, HttpSession session){
        User_account user_account=new User_account();
        user_account.setId((Integer) session.getAttribute("loginId"));//登录人
        user_account.setPassword(bCryptPasswordEncoder.encode(password));
        if(userService.updatePassword(user_account)>0){
            return Result.build(200, "修改成功");
        }
            return Result.build(400, "修改失败");

    }
    /**
     *
     * 退出登录
     */
    @PostMapping("/users/loginout")
    @ApiOperation(value = "账户登出",notes = "未测试")
    public Result loginout(HttpSession session){

        session.removeAttribute("loginId");
        session.removeAttribute("loginAccount");
        return Result.build(200,"退出成功");
    }




}
